package com.tiger.cmpeb.sample4;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegisterFlight extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Integer flightID = new Integer(request.getParameter("flightID"));
			String flightName = request.getParameter("flightName");
			SimpleDateFormat fomatter = new SimpleDateFormat("yy/MM/dd hh:mm");
			Date departureDate = fomatter.parse(request.getParameter("departureDate"));
			String origin = request.getParameter("origin");
			String destination = request.getParameter("destination");

			InitialContext ctx = new InitialContext();
			FlightHome01 flightHome = (FlightHome01) ctx.lookup("comp/env/ejb/FlightBean01");
			flightHome.create(flightID, flightName, departureDate, origin, destination);

			response.sendRedirect("../com_tiger_cmpeb_sample4.html");

		} catch (Exception e) {
			PrintWriter out = response.getWriter();
			out.println("<html><body><pre>");
			e.printStackTrace(out);
			out.println("</pre></body></html>");
			out.close();
		}
	}
}
